package parse

import (
	"strconv"
	"strings"
	"fmt"
)

type ConvErr struct {
	Original string
	Err error
}

func (e *ConvErr) String() string {
	return e.Original + ": " + e.Err.Error()
}

func strs2nums(strs []string) (nums []int) {
	if len(strs)==0 {
		panic("no words to convert")
	}
	for _,str := range strs {
		if num,err:=strconv.Atoi(str); err!=nil {
			panic(&ConvErr{str, err})
		} else {
			nums=append(nums, num)
		}
	}
	return
}

func Parse(input string) (nums []int, err error) {
	defer func(){
		if p:=recover(); p!=nil {
			var isError bool
			err,isError=p.(error)
			if(!isError) {
				err=fmt.Errorf("%v", p)
			}

		}
	}()
	strs:=strings.Fields(input)
	nums=strs2nums(strs)
	return
}
